def f():
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print('1' + "\n" + '1' + "\n" + '1' + "\n")
return
visited = [False] * n
branch = [True] * n
for i in a:
branch[i-1] = False
print(branch.count(True))
for i in range(n):
if branch[i]:
t = i
path = [i+1]
while not visited[a[t]-1]:
visited[a[t]-1] = True
path.append(a[t])
t = a[t]-1
print(len(path))
print(' '.join(str(x) for x in path[::-1]))
print()
for _ in range(int(input())):
f()
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int a[N];
int used[N];
vector<int>leaves;
stack<int>s;
void solve()
{
int n;
cin>>n;
leaves.clear();
for(int i=1;i<=n;i++) used[i]=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
used[a[i]]=1;
}
if(n==1)
{
cout<<"1\n1\n1\n\n";
return;
}
for(int i=1;i<=n;i++)
{
if(!used[i]) leaves.push_back(i);
}
int siz=leaves.size();
cout<<siz<<'\n';
for(int i=1;i<=n;i++) used[i]=0;
for(auto &i:leaves)
{
int cnt=0;
int noww=i;
while(!used[noww])
{
cnt++;
used[noww]=1;
s.push(noww);
if(a[noww]==noww) break;
else noww=a[noww];
}
if(cnt>0)
{
cout<<cnt<<'\n';
cout<<s.top();
s.pop();
while(!s.empty())
{
cout<<" "<<s.top();
s.pop();
}
cout<<'\n';
}
}
cout<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--) solve();
return 0;
}
97. Interleaving String | 543. Diameter of Binary Tree |
124. Binary Tree Maximum Path Sum | 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts |
501A - Contest | 160A- Twins |
752. Open the Lock | 1535A - Fair Playoff |
1538F - Interesting Function | 1920. Build Array from Permutation |
494. Target Sum | 797. All Paths From Source to Target |
1547B - Alphabetical Strings | 1550A - Find The Array |
118B - Present from Lena | 27A - Next Test |
785. Is Graph Bipartite | 90. Subsets II |
1560A - Dislike of Threes | 36. Valid Sudoku |
557. Reverse Words in a String III | 566. Reshape the Matrix |
167. Two Sum II - Input array is sorted | 387. First Unique Character in a String |
383. Ransom Note | 242. Valid Anagram |
141. Linked List Cycle | 21. Merge Two Sorted Lists |
203. Remove Linked List Elements | 733. Flood Fill |